import { defineStore } from 'pinia'
import { http } from '@/utils/request'
import type { BannerList, CategoryList, NewGoodsList } from '@/types'

export const useHomeStore = defineStore('home', {
  persist: {
    storage: localStorage,
    key: 'HomeList',
  },
  state() {
    return {
      categoryList: [] as CategoryList,
      bannerList: [] as BannerList,
      newGoodsList: [] as NewGoodsList,
    }
  },
  actions: {
    async getAllCategory() {
      const res = await http<CategoryList>('/home/category/head', 'get')
      this.categoryList = res.data.result
    },
    async getBannerList() {
      const res = await http<BannerList>('/home/banner', 'GET')
      this.bannerList = res.data.result
    },
    async getNewGoodsList() {
      const res = await http<NewGoodsList>('/home/new', 'GET', { limit: 4 })
      this.newGoodsList = res.data.result
      console.log('/home/new', res)
    },
  },
})
